package com.chengshuang.log.fuction.task;

import com.chengshuang.log.component.DiffParseComponent;
import com.chengshuang.log.context.LogRecordContext;
import com.chengshuang.log.domain.ProjectTask;
import com.chengshuang.log.fuction.LogRecordFunction;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * @author shuang.cheng
 * @date 2022/10/13
 * @description
 */
@Component
public class TaskUpdateLogFunction implements LogRecordFunction {

    private final String FUNCTION_NAME = "task_update";

    @Resource
    private DiffParseComponent diffParseComponent;

    @Override
    public String functionName() {
        return FUNCTION_NAME;
    }

    @Override
    public String apply(Object value) {
        List<Object> snapshots = LogRecordContext.getSnapshotCache();
        if (CollectionUtils.isEmpty(snapshots)) {
            return null;
        }
        Object snapshot = snapshots.get(0);
        ProjectTask oldTask;
        if (snapshot instanceof ProjectTask) {
            oldTask = (ProjectTask) snapshot;
        } else {
            return null;
        }
        ProjectTask task;
        if (value instanceof ProjectTask) {
            task = (ProjectTask) value;
        } else {
            return null;
        }

        return diffParseComponent.diff(oldTask, task);
    }
}
